/**
* 作者: liubaoyuan
* 日期: 2024-10-25 10:46:14
* 备注: 玩家约会表
*/
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.commondata.bag.ItemId;
import com.motu.monstercity.server.game.commondata.system.UserExp;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.log.MotuLogManager;
import com.motu.monstercity.server.game.logic.GameDataManager;
import com.motu.monstercity.server.game.logic.GameUser;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import io.vertx.core.json.JsonObject;

public class UserDating extends UserDatingModel {
	public UserDating() {
		super();
	}

	public JsonObject insert() {
		UserInfo userInfo = GameUser.getUserInfo(getId());
		int max = takeMaxStrength(userInfo);// 自动恢复的体力上限
		putStrength(max);
		putRecoverTime(TimeUtils.getCurTime());
		return super.insert();
	}

	public void reset() {
		boolean flag = false;
		if (recoverEnergy())  {
			flag = true;
		}
		if (flag) {
			update();
		}
	}

	// 玩家的精力上限
	public int takeMaxStrength(UserInfo userInfo) {
		UserExp userExp = GameDataManager.getUserExp(userInfo.getLevel());
		if (userExp == null) {
			return 0;
		} else {
			return userExp.getBeautyNum();
		}
	}

	// 约会体力的恢复cd时间
	public int takeCdTime(UserInfo userInfo) {
		UserExp userExp = GameDataManager.getUserExp(userInfo.getLevel());
		if (userExp == null) {
			return 0;
		} else {
			return userExp.getBeautyCdTime();
		}
	}

	// 记录日志
	public void addStrength(UserInfo userInfo, int num, Object... params) {
		int balance = getStrength();// 当前余额
		long dbid = 0;
		MotuLogManager.logItemIncome(userInfo, ItemId.DATING_ENERGY, dbid, balance, num, params);
		super.addStrength(num);
	}

	// 扣除体力
	public void subStrength(UserInfo userInfo, int num, Object... params) {
		int balance = getStrength();// 当前余额
		if (num > balance) {
			num = balance;// 余额不足
		}
		int max = takeMaxStrength(userInfo);// 上限
		if (balance >= max && balance - num < max) {// 原来的体力超过上限,且使用后低于下限,开始cd倒计
			putRecoverTime(TimeUtils.getCurTime());
		}
		long dbid = 0;
		MotuLogManager.logItemConsume(userInfo, ItemId.DATING_ENERGY, dbid, balance, num, params);
		super.addStrength(-num);
	}

	// 恢复体力
	public boolean recoverEnergy() {
		UserInfo userInfo = GameUser.getUserInfo(getId());
		int max = takeMaxStrength(userInfo);// 自动恢复的体力上限
		int cdtime = takeCdTime(userInfo);  //体力恢复cd时间
		long diff = TimeUtils.getTimeDiff(getRecoverTime());
		if (max == 0 || cdtime == 0 || diff < cdtime || getStrength() >= max) {
			return false;// 不需要执行恢复逻辑
		}

		int addNum = (int) Math.floor((double) diff / cdtime);  // 恢复体力点数
		if (getStrength() + addNum < max) {
			putRecoverTime(getRecoverTime() + cdtime * addNum);// // 恢复了n点, 标记时间就加N个cdtime
		} else {
			addNum = max - getStrength();
			putRecoverTime(TimeUtils.getCurTime());
		}
		addStrength(userInfo, addNum, LogType.DATING_ENERGY_AUTO);
		return true;
	}
}
